<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket 路径放行测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .status {
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
            font-weight: bold;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        .log-area {
            height: 300px;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 10px;
            background-color: #f8f9fa;
            font-family: monospace;
            font-size: 12px;
            border-radius: 4px;
            margin-top: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>WebSocket 路径放行测试</h1>
        
        <div class="status info" id="status">
            准备测试 WebSocket 连接...
        </div>
        
        <div class="form-group">
            <label for="wsUrl">WebSocket URL:</label>
            <input type="text" id="wsUrl" value="ws://localhost:7081/ws/message" placeholder="WebSocket URL">
        </div>
        
        <div class="form-group">
            <label for="token">Token:</label>
            <input type="text" id="token" value="871ee45db8b443b048a12d5fcc61038b" placeholder="Token">
        </div>
        
        <button onclick="testConnection()" id="testBtn">测试连接</button>
        <button onclick="testWithoutToken()" id="testNoTokenBtn">测试无Token连接</button>
        <button onclick="clearLog()" id="clearBtn">清空日志</button>
        
        <div class="log-area" id="logArea"></div>
    </div>

    <script>
        let ws = null;
        
        function log(message, type = 'info') {
            const logArea = document.getElementById('logArea');
            const logItem = document.createElement('div');
            logItem.style.color = type === 'error' ? 'red' : type === 'success' ? 'green' : 'black';
            logItem.textContent = new Date().toLocaleTimeString() + ': ' + message;
            logArea.appendChild(logItem);
            logArea.scrollTop = logArea.scrollHeight;
        }
        
        function updateStatus(message, type = 'info') {
            const status = document.getElementById('status');
            status.textContent = message;
            status.className = 'status ' + type;
        }
        
        function testConnection() {
            const url = document.getElementById('wsUrl').value;
            const token = document.getElementById('token').value;
            
            if (!url || !token) {
                updateStatus('请输入 URL 和 Token', 'error');
                return;
            }
            
            updateStatus('正在测试连接...', 'info');
            log('开始测试 WebSocket 连接');
            log('URL: ' + url);
            log('Token: ' + token);
            
            try {
                // 创建 WebSocket 连接
                ws = new WebSocket(url);
                
                ws.onopen = function(event) {
                    updateStatus('WebSocket 连接成功！Spring Security 已正确放行', 'success');
                    log('✅ WebSocket 连接已建立', 'success');
                    log('✅ Spring Security 配置正确，/ws/** 路径已被放行', 'success');
                    
                    // 发送测试消息
                    const testMessage = {
                        action: 'testStream',
                        data: {}
                    };
                    ws.send(JSON.stringify(testMessage));
                    log('发送测试消息: ' + JSON.stringify(testMessage));
                };
                
                ws.onmessage = function(event) {
                    log('收到消息: ' + event.data, 'success');
                };
                
                ws.onclose = function(event) {
                    updateStatus('WebSocket 连接已关闭', 'info');
                    log('WebSocket 连接已关闭，代码: ' + event.code + ', 原因: ' + event.reason);
                };
                
                ws.onerror = function(error) {
                    updateStatus('WebSocket 连接失败！可能是 Spring Security 阻止了连接', 'error');
                    log('❌ WebSocket 连接错误: ' + error, 'error');
                    log('❌ 请检查 Spring Security 配置是否正确放行了 /ws/** 路径', 'error');
                };
                
            } catch (error) {
                updateStatus('连接创建失败: ' + error.message, 'error');
                log('❌ 连接创建失败: ' + error.message, 'error');
            }
        }
        
        function testWithoutToken() {
            const url = document.getElementById('wsUrl').value;
            
            if (!url) {
                updateStatus('请输入 URL', 'error');
                return;
            }
            
            updateStatus('正在测试无Token连接...', 'info');
            log('开始测试无Token WebSocket 连接');
            log('URL: ' + url);
            
            try {
                ws = new WebSocket(url);
                
                ws.onopen = function(event) {
                    updateStatus('无Token连接成功！但建议使用Token进行身份验证', 'success');
                    log('✅ 无Token WebSocket 连接已建立', 'success');
                    log('⚠️ 注意：虽然连接成功，但建议使用Token进行身份验证', 'error');
                };
                
                ws.onmessage = function(event) {
                    log('收到消息: ' + event.data, 'success');
                };
                
                ws.onclose = function(event) {
                    updateStatus('无Token连接已关闭', 'info');
                    log('无Token WebSocket 连接已关闭，代码: ' + event.code);
                };
                
                ws.onerror = function(error) {
                    updateStatus('无Token连接失败', 'error');
                    log('❌ 无Token WebSocket 连接错误: ' + error, 'error');
                };
                
            } catch (error) {
                updateStatus('无Token连接创建失败: ' + error.message, 'error');
                log('❌ 无Token连接创建失败: ' + error.message, 'error');
            }
        }
        
        function clearLog() {
            document.getElementById('logArea').innerHTML = '';
        }
        
        // 页面加载完成后的初始化
        window.onload = function() {
            log('WebSocket 路径放行测试页面已加载');
            log('测试目标：验证 Spring Security 是否正确放行了 /ws/** 路径');
            log('预期结果：WebSocket 连接应该能够成功建立');
        };
    </script>
</body>
</html> 